package global.com.loansapplication.ui.actviity.user.presenter;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

import global.com.loansapplication.base.BaseBiz;
import global.com.loansapplication.bean.CustomInfo;
import global.com.loansapplication.bean.VoiceListInfo;
import global.com.loansapplication.comm.MyApplication;
import global.com.loansapplication.comm.MyType;
import global.com.loansapplication.event.CloseActEvent;
import global.com.loansapplication.event.RechargeEvent;
import global.com.loansapplication.ui.actviity.user.biz.CustomBiz;
import global.com.loansapplication.ui.actviity.user.contract.CustomContract;

/**
 * Created by Administrator on 2017/10/21.
 */

public class CustomPresenter implements CustomContract.Presenter {

    CustomContract.View view;
    CustomBiz customBiz;


    @Inject
    public CustomPresenter(CustomContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        customBiz = (CustomBiz) biz;
    }

    @Override
    public void addCollection(String uid) {
        view.loading(true);
        customBiz.addColl(MyApplication.userInfo.getUserCode(), uid, new BaseBiz.Callback<CustomInfo>() {
            @Override
            public void onSuccess(CustomInfo o) {
                view.showMsg("收藏成功");
                view.loading(false);
                view.setCol(true, o.getUid());
                EventBus.getDefault().post(new CloseActEvent(MyType.collectioon));
            }

            @Override
            public void onFailure(CustomInfo o) {
                view.loading(false);
            }
        });
    }

    @Override
    public void delCollection(String studentCode) {
        view.loading(true);
        customBiz.delColl(MyApplication.userInfo.getUserCode(), studentCode, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object o) {
                view.showMsg("取消收藏成功");
                view.loading(false);
                view.setCol(false, "");
                EventBus.getDefault().post(new CloseActEvent(MyType.collectioon));
            }

            @Override
            public void onFailure(Object o) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getInfo(String studentCode, String useable) {
        view.loading(true);
        customBiz.getIndo(MyApplication.userInfo.getUserCode(), studentCode, "1", new BaseBiz.Callback<CustomInfo>() {
            @Override
            public void onSuccess(CustomInfo o) {
                view.upDateUI(o);

                view.loading(false);
            }

            @Override
            public void onFailure(CustomInfo o) {
                view.loading(false);
            }
        });
    }

    @Override
    public void selectVoice() {
        view.loading(true);
        customBiz.selectVoice(view.curContext(), view.getSelectVoice(), new BaseBiz.Callback<List<VoiceListInfo>>() {
            @Override
            public void onSuccess(List<VoiceListInfo> mVoiceListInfoList) {
                view.showSelectVoice(mVoiceListInfoList);
                view.loading(false);
            }

            @Override
            public void onFailure(List<VoiceListInfo> mVoiceListInfoList) {
                view.showFalseSelectVoice();
                view.loading(false);
            }
        });
    }

    @Override
    public void getPayInfo(String studentCode, String useable) {
        view.loading(true);
        customBiz.getIndo(MyApplication.userInfo.getUserCode(), studentCode, "1", new BaseBiz.Callback<CustomInfo>() {
            @Override
            public void onSuccess(CustomInfo o) {
                view.upDateUI(o);
                view.checkPay(o.getUseCount());
                view.loading(false);
            }

            @Override
            public void onFailure(CustomInfo o) {
                view.loading(false);
            }
        });
    }


    @Override
    public void getOrderId(String studentCode, String orderAmount) {

        view.loading(true);
        customBiz.createOrder(MyApplication.userInfo.getUserCode(), studentCode, orderAmount, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object o) {
                view.showSuccess();
                view.loading(false);
                EventBus.getDefault().post(new RechargeEvent());
            }

            @Override
            public void onFailure(Object o) {
                view.loading(false);
            }
        });
    }


}
